package com.lsy.weizhi2.activity;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.SelfMessage;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.adapter.MySelf2Adapter;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyHeight;
import com.lsy.weizhi2.mybase.MyBaseActivity;
import com.lsy.weizhi2.mybase.MyTitleView;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MySelfMessage2 extends MyBaseActivity {
    private Context context;
    private MyTitleView myself_2_title;
    private RecyclerView myself_2_rv;
    private MySelf2Adapter mySelf2Adapter;
    private EditText myself_2_msg;
    private TextView myself_2_tv;
    private List<SelfMessage.ListBean> list;
    private Gson gson;
    private int pageNo,xx;
    private String friendId;
    private Handler handler=new Handler();
    private boolean isShow;
    private Runnable run = new Runnable() {
        @Override
        public void run() {
            if(isShow){
                getData();
                handler.postDelayed(run,60000);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_self_message2);
        Init();
    }

    private void Init() {
        context=MySelfMessage2.this;
        gson=new Gson();
        list=new ArrayList<>();
        myself_2_title= (MyTitleView) findViewById(R.id.myself_2_title);
        myself_2_rv= (RecyclerView) findViewById(R.id.myself_2_rv);
        myself_2_msg= (EditText) findViewById(R.id.myself_2_msg);
        myself_2_tv= (TextView) findViewById(R.id.myself_2_tv);
        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        myself_2_rv.setLayoutManager(manager);
        mySelf2Adapter = new MySelf2Adapter(context,list);
        myself_2_rv.setAdapter(mySelf2Adapter);
        myself_2_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg=myself_2_msg.getText().toString();
                if(msg!=null&&msg.length()!=0){
                    SendMsg(msg);
                }else{
                    MyMethod.ShowDialog(context,"内容不能为空");
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        friendId=getIntent().getStringExtra("friendId");
        Log.i("MySelfMessage2","friendId=="+friendId);
        pageNo=1;
        getData();
        xx = 0;
        ViewTreeObserver vto = myself_2_title.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (xx < 1) {
                    xx++;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(myself_2_title.getLayoutParams());
                            params.height = MyHeight.ACTIONBAR + MyHeight.MyTitleHeight;
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                                myself_2_title.setLayoutParams(params);
                            }
                        }
                    });
                }
            }
        });
        isShow=true;
    }

    private void getData(){
        Map<String,String>map=new HashMap<>();
        map.put("pageNo",""+pageNo);
        map.put("pageSize","20");
        MyOkHttpUtils.getInstance(context).doGet(Constants.SelectMessageList+friendId+"/private/message",null)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {

                    }

                    @Override
                    public void onResponse(String message) {
                        Log.i("MySelfMessage2","message==sss="+message);
                        if(message!=null){
                            try {
                                JSONObject jsonObject=new JSONObject(message);
                                boolean result = jsonObject.getBoolean("result");
                                if(result){
                                    if(pageNo==1){
                                        list.clear();
                                    }
                                    SelfMessage selfMessage=gson.fromJson(message,SelfMessage.class);
                                    list.addAll(selfMessage.getList());
                                    Log.i("MySelfMessage2","list==sss="+list.size());
                                    mySelf2Adapter.UpData(list,0,0);
                                    myself_2_rv.scrollToPosition(list.size()-1);
                                }
                                handler.postDelayed(run,60000);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

    private void SendMsg(String msg) {
        handler.removeCallbacks(run);
        Map<String,String> map=new HashMap<>();
        map.put("friendId",friendId+"");
        map.put("messageContent",msg);
        MyOkHttpUtils.getInstance(context).doPost(Constants.SendPrivateMessage,map)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {

                    }

                    @Override
                    public void onResponse(String message) {
                        if(message!=null){
                            try {
                                JSONObject jsonObject=new JSONObject(message);
                                boolean result = jsonObject.getBoolean("result");
                                if(result){
                                    myself_2_msg.setText("");
                                    getData();
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }
}
